package com.school.chapter01.homework;


import java.util.Scanner;

@SuppressWarnings({"all"})
class Test {
    public static void main(String[] args) {


        boolean flag = true;
        Scanner scanner = new Scanner(System.in);
        System.out.printf("男生总人数：");
        int boyNo = scanner.nextInt();
        System.out.print("女生总人数(男女生人数不能为倍数关系)：");
        int girlNo = scanner.nextInt();
        CircleLinkedList_ boy = new CircleLinkedList_();
        CircleLinkedList_ girl = new CircleLinkedList_();
        boy.creatPeoples(boyNo);
        girl.creatPeoples(girlNo);
        while (flag) {
            System.out.println("\n请选择：\n1.输入歌号，返回从一开始到那首歌的所有配对情况\n2.输入两人，返回歌号\n-1退出");
            int i = scanner.nextInt();
            switch (i) {
                case 2:
                    System.out.printf("男生中的：");
                    int p1 = scanner.nextInt();
                    System.out.printf("和女生中的：");
                    int p2 = scanner.nextInt();
                    System.out.printf("一起跳舞时，是第%s首歌", Dancer.pair(boy, girl, p1, p2));
                    break;
                case 1:
                    System.out.printf("输入歌号：");
                    int n = scanner.nextInt();
                    Dancer.show(boy, girl, n);
                    break;
                case -1:
                    System.out.println("已退出");
                    flag = false;
                    scanner.close();
                    break;
                default:
                    throw new RuntimeException("输入错误");
            }
        }

    }
}

public class Dancer {
    public static String pair(CircleLinkedList_ boy, CircleLinkedList_ girl, int p1, int p2) {

        CircleLinkedList_.People cur1 = boy.getHeader();
        CircleLinkedList_.People cur2 = girl.getHeader();
        int i1;
        int i2;
        int count1 = 0;
        int count2 = 0;
        int n = 0;
        for (int count = 1; ; count++) {
            i1 = boy.getNo(cur1);
            i2 = girl.getNo(cur2);
            if (i1 == p1 && i2 == p2) {
                n++;
                if (n == 1) {
                    count1 = count;
                }
                if (n == 2) {
                    count2 = count;
                    return count1 + " 和 " + count2;
                }
            }
            cur1 = cur1.next;
            cur2 = cur2.next;

        }
    }

    public static void show(CircleLinkedList_ boy, CircleLinkedList_ girl, int i) {
        CircleLinkedList_.People cur1 = boy.getHeader();
        CircleLinkedList_.People cur2 = girl.getHeader();
        for (int count = 1; count <= i; count++) {
            System.out.printf("\n======第%d次======\n男生的%d 和 女生的%d 一起跳舞", count, cur1.getNo(), cur2.getNo());
            cur1 = cur1.next;
            cur2 = cur2.next;
        }
    }

}

class CircleLinkedList_ {
    public class People {
        int No;
        People next;

        public People(int no) {
            No = no;
        }

        public int getNo() {
            return No;
        }

        public void setNo(int no) {
            No = no;
        }

        public People getNext() {
            return next;
        }

        public void setNext(People next) {
            this.next = next;
        }
    }

    private People header;
    private int size;

    public void creatPeoples(int size) {
        if (size < 1) {
            throw new RuntimeException("size的值不正确");
        }
        People curPeople = null;
        for (int i = 1; i <= size; i++) {
            People people = new People(i);
            if (i == 1) {
                header = people;
                header.setNext(header);//形成环
                curPeople = header;
            } else {
                curPeople.setNext(people);
                people.setNext(header);
                curPeople = people;
            }
        }
    }

    public int getNo(People p) {
        return p.getNo();
    }

    public People getHeader() {
        return header;
    }

    public void setHeader(People header) {
        this.header = header;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }


}

